#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    Node *next;
} Node;

Node *getNode(int data)
{
    static Node *node;
    node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    return node;
}

void append(Node *first_node, int data)
{
    Node *tmp = first_node;
    while (tmp->next != NULL)
    {
        tmp = (Node *)tmp->next;
    }
    Node *next_node = getNode(data);
    tmp->next = next_node;
}
void print_list(Node *node)
{
    Node *tmp = node;
    while (tmp->next != NULL)
    {
        printf("%d\n", tmp->data);
        tmp = tmp->next;
    }
    printf("%d\n", tmp->data);
}

void insert(Node *first_node, int index, int data)
{
    Node *tmp;
    tmp = first_node;
    index--;
    for (; index > 0; index--)
    {
        if (tmp->next != NULL)
        {
            tmp = tmp->next;
        }
        else
        {
            break;
        }
    }
    Node *ins = getNode(data);
    ins->next = tmp->next;
    tmp->next = ins;
}

void del(Node *first_node, int index)
{
    if (first_node == NULL)
    {
        return;
    }
    index--;
    Node *tmp;
    tmp = first_node;
    for (; index > 1; index--)
    { //要删除的上一个
        if (tmp->next != NULL)
        {
            tmp = tmp->next;
        }
        else
            break;
    }
    Node *p2 = tmp->next;
    tmp->next = tmp->next->next;
    free(p2);
}

int main(int argc, char const *argv[])
{
    Node *first;
    first = getNode(0);
    append(first, 1);
    append(first, 5);
    append(first, 8);
    insert(first, 1, 666);
    del(first, 2);
    print_list(first);
    return 0;
}
